package com.dc.di.transport.webspheremq;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.log4j.Logger;
import org.pentaho.di.www.BaseHttpServlet;

public class MQCommandServlet extends BaseHttpServlet {

	private static final Logger logger = Logger.getLogger(MQCommandServlet.class);
	
	private static final long serialVersionUID = -5527628092153762931L;

	public static ConfigurationContext AXIS_CONFIG = null;
	
	public static final String CONTEXT_PATH = "/MQCommandServlet";
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			if(logger.isInfoEnabled()){
				logger.info(new StringBuilder("Execute command '").append(request.getParameter("command")).append("'"));
			}
			String responsevalue = "ok";
			if(command(request,"pause")){
				MQConfig c = getConfig(request);
				QueueListener.pause(c.ip,c.port,c.queueManagerName,c.channel,c.queueName,c.ccsid);
			}else if(command(request,"unpause")){
				MQConfig c = getConfig(request);
				QueueListener.cancelPause(c.ip,c.port,c.queueManagerName,c.channel,c.queueName,c.ccsid);
			}else if(command(request,"listenerSwitch")){
				MQConfig c = getConfig(request);
				QueueListener.startOneoff(c.ip,c.port,c.queueManagerName,c.channel,c.ccsid,c.queueName,AXIS_CONFIG,300);
			}else if(command(request,"status")){
				MQConfig c = getConfig(request);
				if(!QueueListener.isRunning(c.ip,c.port,c.queueManagerName,c.channel,c.queueName,c.ccsid)){
					responsevalue = "stop";
				}
			}else if(command(request,"exit")){
				MQConfig c = getConfig(request);
				QueueListener.stop(c.ip,c.port,c.queueManagerName,c.channel,c.queueName,c.ccsid);
			}else{
				responsevalue = "error command " + request.getParameter("command");
			}
			if(logger.isInfoEnabled()){
				logger.info(new StringBuilder("Successful execute command '").append(request.getParameter("command")).append("' return value is [").append(responsevalue).append("]"));
			}
			response.getWriter().print(responsevalue);
		} catch (Exception e) {
			logger.error(new StringBuilder("Execute command '").append(request.getParameter("command")).append("' occur error : ").append(e.getMessage()));
			response.getWriter().print("Error:"+e.getMessage());
		}
	}
	
	protected boolean command(HttpServletRequest request,String command){
		String mcd = request.getParameter("command");
		if(mcd!=null){
			return mcd.trim().equalsIgnoreCase(command);
		}
		return false;
	}
	
	protected MQConfig getConfig(HttpServletRequest request){
		MQConfig config = new MQConfig();
		config.ip = getParamNotnull(request,"ip");
		config.port = Integer.parseInt(getParamNotnull(request,"port"));
		config.queueManagerName = getParamNotnull(request,"manager");
		config.queueName = getParamNotnull(request,"queue");
		config.channel = getParamNotnull(request, "channel");
		if(request.getParameter("ccsid")!=null){
			config.ccsid = Integer.parseInt(getParamNotnull(request,"ccsid"));
		}
		if(request.getParameter("threadcount")!=null){
			config.threadcount = Integer.parseInt(getParamNotnull(request,"threadcount"));
		}
		if(request.getParameter("interval")!=null){
			config.interval = Integer.parseInt(getParamNotnull(request,"interval"));
		}
		if(logger.isInfoEnabled()){
			logger.info(new StringBuilder("Load config from 'servlet request' is ").append(config.toString()));
		}
		return config;
	}
	
	/*protected String listenerSwitch(HttpServletRequest request){
		String response = "ok";
		try {
			MQConfig c = getConfig(request);
			QueueListener.startOneoff(c.ip,c.port,c.queueManagerName,c.channel,c.ccsid,c.queueName,AXIS_CONFIG,300);
		} catch (Exception e) {
			response = "Error:"+e.getMessage();
			throw
		}
		return response;
	}*/
	
	protected String getParamNotnull(HttpServletRequest request,String name){
		String ret  = request.getParameter(name);
		if(ret==null || ret.trim().length()==0){
			throw new IllegalArgumentException("Parameter "+name+" is null .");
		}
		return ret;
	}
		
}
